@echo off
setlocal enabledelayedexpansion

REM 检查 Ollama 是否运行
curl http://localhost:11434/api/tags >nul 2>nul
if %errorlevel% neq 0 (
    echo [INFO] Ollama 未运行，尝试启动...
    start /min cmd /c "ollama run qwen3:8B > ollama.log 2>&1"
    timeout /t 10 >nul
)

REM 检查 FastAPI 服务是否已运行
for /l %%i in (1,1,10) do (
    curl http://localhost:8000/health >nul 2>nul
    if !errorlevel! equ 0 (
        echo [INFO] FastAPI 服务已就绪
        goto :start
    )
    if %%i lss 10 (
        timeout /t 2 >nul
    )
)

:start
REM 启动 FastAPI 服务
start /min cmd /c "uvicorn app.main:app --port 8000 --log-level info > server.log 2>&1"

REM 等待服务健康
for /l %%i in (1,1,10) do (
    curl http://localhost:8000/health >nul 2>nul
    if !errorlevel! equ 0 (
        echo [INFO] FastAPI 服务已就绪
        exit /b 0
    )
    timeout /t 2 >nul
)
echo [ERROR] FastAPI 服务启动失败
exit /b 1 